<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>	
<%@page import="com.yaodian100.mall.member.util.UserData"%>
<%@page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>	
<script type="text/javascript" charset="UTF-8" src="/js/yaodian_zipcode.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.cascade.ext.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.cascade.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.templating.js"></script>
<SCRIPT type="text/javascript" src="/js/jquery-ui.min.js"></SCRIPT> 
<SCRIPT type="text/javascript" src="/js/jquery.tooltip.min.js"></SCRIPT>

<script language="javascript">
$().ready(function(){
      if ($.browser.msie && $.browser.version ==6) {
      	$("#dialog").removeClass('voucher-area');
      }else{
    	  $("#dialog").addClass('voucher-area');
      }
	$("#dialog").dialog({
		bgiframe: true,
		height: 'auto',
		width:700,
		modal: true
	});

	if($("#couponSize").val()==0){
		$("#dialog").css("height", "auto");
	}
	$("div[id*='coupleProduct_']").each(function(ii){
		$(this).show();
		$("#licouple_"+ (ii+1)).removeClass('bg-yl');
	});
	
	$("#licouple_1").addClass('bg-yl'); 
	var sumSelCoupon="";
	var finalchkCpProdFaceVaule=parseFloat(0);	
	var tem="777777";
	$(":input[id*='selCoupon_']").change(function(i){
		
		chkSelected($(this));
		
		if ($("#rec_"+$(this).attr("id")).val() != $(this).find('option:selected').attr("id")) {
		  	setSelected($(this));
		  	
		  	$("#rec_"+$(this).attr("id")).val($(this).find('option:selected').attr("id"));
		}
	});
	$("#submitDialog").click(function(){
		var finalchkCpCode="";
		var finalRadioId ="";
		$("input[id*='couple_']").each(function(i){
			if(this.checked){
				finalchkCpCode=$(this).attr("value");	
				finalRadioId = $(this).attr("id").split("_")[1];
			}	
		});	
		closeDialog();
	});
});

//設定已選
function setSelected(o){
	$(":input[id*='selCoupon_']").each(function(i){
		if ($(this).attr("id") != o.attr("id")) {
			jQuery('option', this).each(function(q){
				if (q !=0) {
					if ($(this).attr("id") == o.find('option:selected').attr("id")) {
						$(this).attr("disabled","disabled");
						$(this).css('color', '#CCC');
						$(this).text($(this).text()+'(已被使用)');
					}
					if ($(this).attr("id") == $("#rec_"+o.attr("id")).val()) {
						$(this).attr("disabled","");
						$(this).css('color', '#000000');	
						$(this).text($(this).text().replace('(已被使用)',''));
					}
				}
			});
		}
	});
	recalculate();
}
function chkSelected(o){
	$(":input[id*='selCoupon_']").each(function(i){
		if ($(this).attr("id") != o.attr("id")) {
			if ($(this).find('option:selected').attr("id")!="" && $(this).find('option:selected').attr("id") == o.find('option:selected').attr("id")) {
				jQuery('option', o).each(function(q){
				  if ($(this).attr("id") == $("#rec_"+o.attr("id")).val()) {
					  o.attr("selectedIndex",q);
					  return ;
				  }
				});
			}
			
		}
	});
	
}

//重新計算
function recalculate(){
	var countSelectedCoupon =parseFloat(0);
	var selCouponText="";
	sumSelCoupon="";
	finalchkCpProdFaceVaule=parseFloat(0);
	countSelectedCoupon =parseFloat(0);
	$("input[id*='couple_']").each(function(i){
		var finalRadioId = $(this).attr("id").split("_")[1];
		var selCoupon=$("#selCoupon_" + finalRadioId).find('option:selected').val()+";";
		if(selCoupon!=";"){
			sumSelCoupon=sumSelCoupon+selCoupon;
			selCouponText=selCouponText+$("#selCoupon_" + finalRadioId).find('option:selected').val().split(",")[0]+","+$("#selCoupon_" + finalRadioId).find('option:selected').val().split(",")[1]+";";
			var oneCoupon=parseFloat(selCoupon.split(",")[2]);
			finalchkCpProdFaceVaule=parseFloat(finalchkCpProdFaceVaule+oneCoupon);
			countSelectedCoupon=countSelectedCoupon+1;
			$("#showCoupTotal").html(finalchkCpProdFaceVaule);
			$("#showCountSeletedCoupon").html(countSelectedCoupon);
		}else{
			$("#showCoupTotal").html(finalchkCpProdFaceVaule);
			$("#showCountSeletedCoupon").html(countSelectedCoupon);
		}
		$("#showCpCode").val(sumSelCoupon);
	});
	$("#editCoupon").val(parseFloat(finalchkCpProdFaceVaule).toFixed(1));
	ProcessCalcuTotal();
}


</script>
<input type="hidden" id="couponSize" value="${fn:length(couponitems)}"/>
<c-rt:set var="coupons" value="0"/>
<c-rt:choose>
			<c-rt:when test="${fn:length(couponitems)>0}">
			<div id="hideTitle"> <h3>请选择您要使用的礼券及折抵商品：<span>(以下仅列出可折抵此笔订单的礼券)</span></h3></div>
				     <c-rt:forEach items="${couponitems}" var="couponItem" varStatus="rows_index">  
						      <c:if test="${fn:length(rsmap[couponItem.code])>0}">
						      <c-rt:set var="coupons" value="${coupons+1}"/>
									<ul id="ulcouple_${rows_index.count}">
							      		<li id="licouple_${rows_index.count}"><input id="couple_${rows_index.count}" name="icouple_${rows_index.count}" type="radio" value="${couponItem.code}" style="display: none">折扣<strong>${couponItem.coupon.faceValue}元</strong> - 有效期限：<strong><fmt:formatDate pattern="yyyy/MM/dd HH:mm" value="${couponItem.endDate}" /> </strong>- 礼券编号${couponItem.code}  
										      <div id="coupleProduct_${rows_index.count}" style="display: none">
												      <c:set var="str1" value="${rsmap[couponItem.code]}"/>
												      <select id="selCoupon_${rows_index.count}" name="selCoupon"> <option value="">请选择要折抵之商品</option>
													      <c-rt:forEach begin="1" end="${fn:length(str1)}" step="6" varStatus="rows_index2">				         
														       <c-rt:set var="ff">${(rows_index2.count-1)*6}</c-rt:set>
														     <option id="${str1[5+ff]}" value="${str1[0+ff]},${couponItem.code},${couponItem.coupon.faceValue},${str1[3+ff]},${str1[4+ff]},${str1[2+ff]},${str1[5+ff]}">${str1[1+ff]} ${str1[2+ff]}元</option>				     
													      </c-rt:forEach>
												      </select>
												       <input type="hidden" id="rec_selCoupon_${rows_index.count}" name="recordSelctdvalue" value=""/>
										      </div>
									     </li>
							       </ul>
						      </c:if>
						</c-rt:forEach>
						<p class="txt-m" align="right">共使用了 <strong class="txt-rd" id="showCountSeletedCoupon">0</strong> 张礼券，可优惠 <strong class="txt-rd" id="showCoupTotal">0</strong> 元</p>
						 <c:if test="${coupons == 0}">
						       <script language="javascript">$("#hideTitle").hide();$("#hideWraing").hide();</script>
						      	 <h3 align="center">亲，您目前还没有适用于本张订单的礼券哦！</h3>
								  <p class="c-f"  align="center"><br /><a href="javascript:;" id="submitDialog" class="btn-mid" onFocus="blur()">確定</a></p>
						       </c:if>
				       <div id="hideWraing">
						    <p class="c-f"  align="center"><br /><a href="javascript:;" id="submitDialog" class="btn-mid" onFocus="blur()">提交</a></p> 
  		 			</div>
			</c-rt:when>
			<c-rt:otherwise>
				<h3 align="center">亲，您目前还没有适用于本张订单的礼券哦！</h3>
				  <p class="c-f"  align="center"><br /><a href="javascript:;" id="submitDialog" class="btn-mid" onFocus="blur()">確定</a></p>
			</c-rt:otherwise>
		</c-rt:choose>

		